<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/7/4
 * Time: 10:32
 */
namespace app\admin\controller;
use \think\Request;
use think\Db;
class Tales extends Base
{
    protected $res = array(
        'index'   => array('css' => '2,3,4', 'js' => 'list'),
    );

    /**
     * 首页数据
     * @return mixed
     */
    public function index(){
        $type = input('type');
        $phone = input('phone');
        $map = [];
        if($type&&$type!=""){
            $map['long_type'] = $type;
        }
        if($phone&&$phone!=""){
            $map['long_phone'] = $phone;
        }
        $list = Db::table('up_user_auth_long')
            ->field('long_id,long_type,long_how,long_money,long_phone')
            ->where($map)
            ->select();
        $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
        $this->assign ([
            'list'=>$list,
            'action'=>$action,
            'type'=>$type,
            'phone'=>$phone
        ]);
        return $this->fetch();
    }

    /**
     * add 添加
     * @return mixed|\think\response\Json
     */
    public function add(){
        if(request()->isAjax ()){
            extract(input());
            if($long == ''){
                return $this->api_error('时长不能为空', '14001');
            }
            if($money == ''){
                return $this->api_error('价格不能为空', '14001');
            }
            $param = [
                'long_type'=>$type,
                'long_how'=>$long,
                'long_money'=>$money,
                'long_phone'=>$phone
            ];
            if($phone == 1 && $key == ""){
                return $this->api_error('苹果申请key不能为空', '14001');
            }else{
                $param['long_iphone_id'] = $key;
            }
            $res = Db::table('up_user_auth_long')
                ->insert($param);
            if ($res) {
                return $this->api_success("操作成功");
            } else {
                return $this->api_dbError("操作失败");
            }
        }
        return $this->fetch('add');
    }

    /**
     * 删除
     * @return \think\response\Json
     */
    public function delIndex(){
        $id = input('id');
        $res = Db::table('up_user_auth_long')->delete($id);
        if ($res) {
            return $this->api_success("操作成功");
        } else {
            return $this->api_dbError("操作失败");
        }
    }

    /**
     * 修改召集令
     * @return mixed|\think\response\Json
     */
    public function edit(){
        if(request()->isAjax ()){
            extract(input());
            if($long == ''){
                return $this->api_error('时长不能为空', '14001');
            }
            if($money == ''){
                return $this->api_error('价格不能为空', '14001');
            }
            $param = [
                'long_type'=>$type,
                'long_how'=>$long,
                'long_money'=>$money,
                'long_phone'=>$phone,
                'long_update_time'=>date('Y-m-d H:i:s')
            ];
            if($phone == 1 && $key == ""){
                return $this->api_error('苹果申请key不能为空', '14001');
            }else{
                $param['long_iphone_id'] = $key;
            }
            if($phone ==2 && $key != ""){
                $param['long_iphone_id'] = '';
            }
            $res = Db::table('up_user_auth_long')->where('long_id',$id)->update($param);
            if ($res) {
                return $this->api_success("操作成功");
            } else {
                return $this->api_dbError("操作失败");
            }
        }
        $id = input('id');
        $data = Db::table('up_user_auth_long')
            ->field ('long_id,long_type,long_how,long_money,long_phone,long_iphone_id')
            ->where('long_id',$id)
            ->find();
        $this->assign ('data',$data);
        return $this->fetch('edit');
    }
}